package com.example.mycamera;

import java.io.File;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	public Button btn_photo;
	public ImageView iv_photo;
	public static final int TAKE_PHOTO=1;
	private Uri imageUri;
	private File currentFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        btn_photo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				File dir=new File(Environment.getExternalStorageDirectory(),"myphoto");
				if (!dir.exists()) {
					dir.mkdirs();
				}
				
				currentFile = new File(dir, System.currentTimeMillis() + ".jpg");
				if (!currentFile.exists()) {
					try {
						currentFile.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				in.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentFile));
				startActivityForResult(in,TAKE_PHOTO);

				
			}
		});
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	Log.d("TAG", requestCode + "\n" + resultCode);
	if (requestCode == TAKE_PHOTO) {
		if (resultCode == RESULT_OK) {
			iv_photo.setImageURI(Uri.fromFile(currentFile));
		}else{
			currentFile.delete();
		}
	}
}
	private void init() {
		// TODO Auto-generated method stub
		btn_photo=(Button) findViewById(R.id.btn_photo);
		iv_photo=(ImageView) findViewById(R.id.iv_photo);
	}

}
